//引入mongoose
var mongoose = require('mongoose');
//2 连接数据库了
//f77数据库的名字
//err 错误优先
mongoose.connect('mongodb://localhost/f77',{},(err)=>{
   if(err){
    console.log(err);
    return;
   }
   console.log("数据库连接成功")
});
//创建Schema
let studentSchema = new mongoose.Schema({
    name:String,
    age:Number,
    gender:String,
    score:Number,
    address:String
}) 
//3 创建数据的模型
let studentModel = mongoose.model("studentModel",studentSchema,"students");

//操作数据库了
//添加
/* let res = studentModel.create({name:"小雨",age:18,gender:"女",address:"资中市",score:80});
res.then((data)=>{
    console.log(data)
}) */
//查询
/*
参数1： 条件
参数2： 显示的列名
*/
//查询所有的学生
/* let res1 = studentModel.find({})
res1.then((data)=>{
    console.log(data)
}) */
//查询所有的性别为男的同学
/* let res2 = studentModel.find({"gender":"男"})
res2.then((data)=>{
    console.log(data)
}) */

//查询姓名叫小雨 性别为男的同学
/*  let res3 = studentModel.find({"gender":"男",name:"小雨"})
res3.then((data)=>{
    console.log(data)
}); */
//查询成年的学生
//$gt 大于
/* let res4 = studentModel.find({"age":{$gt:18}})
res4.then((data)=>{
    console.log(data)
}) */
//$gte 大于等于
/* let res4 = studentModel.find({"age":{$gte:18}})
res4.then((data)=>{
    console.log(data)
}) */
//$lt 小于  $lte小于等于
/* let res4 = studentModel.find({"age":{$lt:18}})
res4.then((data)=>{
    console.log(data)
}) */

/* let res4 = studentModel.find({"age":{$lte:18}})
res4.then((data)=>{
    console.log(data)
}) */
//查询年龄大于等于18 并小于26
/* let res4 = studentModel.find({"age":{$gte:18,$lt:26}})
res4.then((data)=>{
    console.log(data)
}) */
/* let res4 = studentModel.find({"age":{$gte:18,$lt:26},"gender":"女"})
res4.then((data)=>{
    console.log(data)
}) */
//查询姓小的同学
/* let res4 = studentModel.find({name:/^小/ig,"gender":"男",address:/资/ig})
res4.then((data)=>{
    console.log(data)
}) */
//查询年龄大于18 或则 性别为女的同学
 /* let res4 = studentModel.find({$or:[{age:{$gt:18}},{gender:"女"}]},["name","age","score"])
res4.then((data)=>{
    console.log(data)
})  */

//把没及格的改为60
/* let res4 = studentModel.updateMany({score:{$lt:60}},{score:60});
res4.then((data)=>{
    console.log(data)
}) */
//排序
//以后在工作中一般都排序（降序）
//默认为升序 1 （asc） -1 降序(desc) 
let res4 = studentModel.find().sort({age:-1,gender:-1})
res4.then((data)=>{
    console.log(data)
})



